package cc.linker.steplink.crm.merchanth5.service;

import cc.linker.steplink.crm.commons.service.CommonsService;
import cc.linker.steplink.crm.merchanth5.dao.VipExchangeDao;
import cc.linker.steplink.crm.response.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Slf4j
@EnableAsync
@Service
public class VipExchangeService {

    @Autowired
    private VipExchangeDao vipExchangeDao;
    @Autowired
    private CommonsService commonsService;
    @Autowired
    private VipExchangeCodeService vipExchangeCodeService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public List<Map> getList(Map<String, Object> param) {
        List<Map> list = vipExchangeDao.getList(param);
        for(Map map : list){
            //relateName
            String relateName = vipExchangeDao.getRelateName(map);
            map.put("relateName",relateName);
        }
        return list;
    }

    public Map getInfo(Long exchangeId) {
        Map map = vipExchangeDao.getInfo(exchangeId);
        if(map!=null && !StringUtils.isEmpty(map.get("exchangeType")) && "2".equals(map.get("exchangeType").toString())){
            List<Map<String, Object>> exchangeCodesList = vipExchangeDao.getExchangeCodesList(exchangeId);
            map.put("exchangeCodesList",exchangeCodesList);
        }
        return map;
    }

    public List<Map> getExChangeList(Map<String, Object> param) {
        List<Map> exChangeList = vipExchangeDao.getExChangeList(param);
        return exChangeList;
    }

    public void saveExchange(Map<String, Object> param) {
        if(StringUtils.isEmpty(param.get("startTime"))){
            param.remove("startTime");
        }
        if(StringUtils.isEmpty(param.get("endTime"))){
            param.remove("endTime");
        }
        if(StringUtils.isEmpty(param.get("exchangeId"))){
            //新增
            if(StringUtils.isEmpty(param.get("exchangeType"))){
                throw GlobalException.build("参数错误");
            }
            Integer exchangeType = Integer.valueOf(param.get("exchangeType").toString());
            Integer count= vipExchangeDao.saveExchange(param);
            if(count!=null && count>0){
                String exchangeId = "0000";
                Integer num = 12;
                if(!StringUtils.isEmpty(param.get("exchangeId"))){
                     exchangeId = param.get("exchangeId").toString();
                     if(exchangeId.length()<=4){
                         switch (exchangeId.length()){
                             case 1:exchangeId="000"+exchangeId;
                             break;
                             case 2:exchangeId="00"+exchangeId;
                                 break;
                             case 3:exchangeId="0"+exchangeId;
                                 break;
                             default:
                                 break;
                         }
                     }else {
                         num = 16 - exchangeId.length();
                     }
                     //添加兑换吗
                    vipExchangeCodeService.addCode(param,exchangeType,exchangeId,num);
                }
            }
        }else {
            //编辑
            vipExchangeDao.updateExchange(param);
        }
    }

    public void deleteExchange(Map<String, Object> param) {
        vipExchangeDao.deleteExchange(param);
        vipExchangeDao.deleteExchangeCode(param);
        String exchangeIdS= param.get("exchangeId").toString();
        if(exchangeIdS.length()<=4){
            switch (exchangeIdS.length()){
                case 1:exchangeIdS="000"+exchangeIdS;
                    break;
                case 2:exchangeIdS="00"+exchangeIdS;
                    break;
                case 3:exchangeIdS="0"+exchangeIdS;
                    break;
                default:
                    break;
            }
        }
        stringRedisTemplate.delete("exchangeCode:exchangeCode_"+exchangeIdS);
    }

    public void updateStatus(Map<String, Object> param) {
        vipExchangeDao.updateStatus(param);
    }

    public void exportExchangeCode(Long exchangeId,Integer exchangeType,String fileName, HttpServletResponse response) {
        List<Map<String,Object>>  exchangeCodesList = vipExchangeDao.getExchangeCodesList(exchangeId);
        for(Map<String,Object> map : exchangeCodesList){
            Integer exchangeStatus = 0;
            if(!StringUtils.isEmpty(map.get("exchangeStatus"))){
                exchangeStatus = Integer.valueOf(map.get("exchangeStatus").toString());
            }
            if(exchangeStatus==0){
                map.put("exchangeStatusString","未兑换");
            }else {
                map.put("exchangeStatusString","已兑换");
            }
            map.put("exchangeDaysString",map.get("exchangeDays")+"天");
        }
        if(exchangeType==1){
            String[] title = {"兑换码", "兑换时长", "是否被兑换"};
            String[] strings = {"exchangeCode", "exchangeDaysString", "exchangeStatusString"};
            try {
                commonsService.excelDownload(exchangeCodesList, strings, title, fileName, response);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            String[] title = {"兑换人手机号","兑换码", "兑换时长", "是否被兑换"};
            String[] strings = {"exchangePhone","exchangeCode", "exchangeDaysString", "exchangeStatusString"};
            try {
                commonsService.excelDownload(exchangeCodesList, strings, title, fileName, response);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void exportTemplate(String fileName, HttpServletResponse response) {
        List<Map<String,Object>>  exchangeCodesList = new ArrayList<>();
        String[] title = {"手机号", "兑换时长(天)"};
        String[] strings = {"exchangePhone","exchangeDays"};
        try {
            commonsService.excelDownload(exchangeCodesList, strings, title, fileName, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
